package com.xunker.appointment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xunker.appointment.model.hospital.Department;
import com.xunker.appointment.repository.DepartmentRepository;
import com.xunker.appointment.service.DepartmentService;
import com.xunker.appointment.vo.hospital.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> objectMap) {
        final String s = JSONObject.toJSONString(objectMap);
        final Department department = JSONObject.parseObject(s, Department.class);
        final Object departmentCode = objectMap.get("departmentCode");
        final Object hospitalCode = objectMap.get("hospitalCode");
        Department department_1=departmentRepository.getDepartmentByDepartmentCodeAndHospitalCode(departmentCode,hospitalCode);
        department.setUpdateTime(new Date());
        department.setDeleted(0);
        if(department_1==null) {
            department.setCreateTime(new Date());
            departmentRepository.insert(department);
        }else {
            departmentRepository.deleteDepartmentByDepartmentCodeAndHospitalCode(departmentCode,hospitalCode);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getByHospitalCode(String hospitalCode, int page, int limit) {
        //创建Pageable对象，设置当前页码和记录数
        Pageable pageable= PageRequest.of(page-1,limit);
        //创建Department对象
        Department department = new Department();
        department.setDeleted(0);
        department.setHospitalCode(hospitalCode);
        //创建Example对象，数据库条件查询
        ExampleMatcher matcher=ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example=Example.of(department,matcher);
        Page<Department> content=departmentRepository.findAll(example,pageable);
        return content;
    }

    @Override
    public void remove(String departmentCode) {
        departmentRepository.deleteDepartmentByDepartmentCode(departmentCode);
    }
//查询科室列表
    @Override
    public List<DepartmentVo> getDepartTree(String hospitalCode) {
        //查询所有科室
        Example<Department> example=Example.of(new Department(hospitalCode));
        List<Department> departments=departmentRepository.findAll(example);
        //stream分组通过bigCode
        final Map<String, List<Department>> collect = departments.stream().collect(Collectors.groupingBy(Department::getBigCode));
        //整理层级结构
        List<DepartmentVo> departmentVos=new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //大科室编号
            String bigcode=entry.getKey();
            //大科室名称
            String bigname=entry.getValue().get(0).getBigName();
            //大科室编号下对应的全部数据
            final List<Department> value = entry.getValue();
            //获取小科室集合
            ArrayList<DepartmentVo> arrayList = new ArrayList<>();
            value.stream().forEach(s->{
                arrayList.add(new DepartmentVo(s.getDepartmentCode(),s.getDepartmentName()));
            });
            departmentVos.add(new DepartmentVo(bigcode,bigname,arrayList));
        }
        return departmentVos;
    }

    @Override
    public Department getDepartmentByHospitalCodeAndDepartmentCode(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHospitalCodeAndDepartmentCode(hoscode, depcode);
    }

    @Override
    public Department getDepartmentByDepartmentCode(String departmentCode) {
        return departmentRepository.getDepartmentByDepartmentCode(departmentCode);
    }
}
